package cn.stylefeng.guns.modular.gridsystem.timer;

import cn.stylefeng.guns.core.timer.TimerTaskRunner;
import cn.stylefeng.guns.modular.gridsystem.controller.BizGatewayTemperatureController;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGateway;
import cn.stylefeng.guns.modular.gridsystem.service.BizCollectorConfigTemplateService;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayService;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayTemperatureService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 网关在线状态定时任务管理
 */
@Component
public class GatewayStatueTaskRunner implements TimerTaskRunner {

    @Resource
    private BizGatewayService bizGatewayService;

    @Resource
    private BizGatewayTemperatureService bizGatewayTemperatureService;

    @Override
    public void action() {
        final List<Integer> list = bizGatewayTemperatureService.listByBeforeTime();
        final List<BizGateway> gatewayList = bizGatewayService.list();
        Set<Integer> set = new HashSet<>();
        gatewayList.forEach(item -> list.forEach(gatewayId->{
            if(item.getId().equals(gatewayId)){
                set.add(gatewayId);
            }
        }));

        for (Integer gatewayId : set) {
            UpdateWrapper<BizGateway> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", gatewayId);
            wrapper.set("online_status", 0);
            bizGatewayService.update(wrapper);
        }
    }

}
